<?php
	//insert an item to the user table
	function db_insert_item_to_user_table($con, $userid, $username, $pass, $email, $valid)
	{
		if (!$con)
		{
			echo "Connect is invalid.<br />";
			return $con;
		}
		$insert_sql = "insert into user (userid, username, password, email, valid)
		values ('$userid', '$username', '$pass', '$email', '$valid')"
		//insert into
		if (mysql_query($insert_sql, $con))
		{
			echo "insert success. <br />";
		}
		else
		{
			echo "insert error<br />";
			return !$con;
		}
		return $con;
	}
	
	//update an item's username to the user table by the userid
	//choose:
	//      0 update username
	//      1 update password
	//      2 update email
	//      3 set valid/invalid
	function db_update_item_to_userby_userid($con, $userid, $updatevalue, $choose)
	{
		if (!$con)
		{
			echo "Connect is invalid.<br />";
			return $con;
		}
		
		//better if use switch-case here
		if ($choose == 0) 
		{
			$update_sql = "update user set username=" . $updatevalue . 
			" where userid=". $userid;
		}
		else if ($choose == 1) 
		{
			$update_sql = "update user set password=" . $updatevalue . 
			" where userid=". $userid;
		}
		else if ($choose == 2)
		{
			$update_sql = "update user set email=" . $updatevalue . 
			" where userid=". $userid;
		}
		else
		{
			$update_sql = "update user set invalid=" . $updatevalue . 
			" where userid=". $userid;
		}
		
		//update
		if (mysql_query($update_sql, $con))
		{
			echo "update success. <br />";
		}
		else
		{
			echo "update error<br />";
			return !$con;
		}
		return $con;
	}
	
	//del item from table.BUT it cannot del an item by some matches
	function db_del_item_from_user_table($con, $delvalue, $choose)
	{
		if (!$con)
		{
			echo "Connect is invalid.<br />";
			return $con;
		}
		
		switch ($choose)
		{
		case 0:
			$del_sql = "delete from user where userid=" . $delvalue;
			break;
		case 1:
			$del_sql = "delete from user where username=" . $delvalue;
			break;
		case 2:
			$del_sql = "delete from user where password=" . $delvalue;
			break;
		case 3:
			$del_sql = "delete from user where email=" . $delvalue;
			break;
		case 4:
			$del_sql = "delete from user where valid=" . $delvalue;
			break;
		default:
			break;
		}
		
		//del
		if (mysql_query($del_sql, $con))
		{
			echo "del success. <br />";
		}
		else
		{
			echo "del error<br />";
			return !$con;
		}
		return $con;
	}
	
?>